/**
 * Author(s): xuming.Wong <xuming.Wong@gmail.com>
 */
#include "M2LoadImpl.h"

#include "GameObject.h"
#include "WOWArchive.h"

#include "M2MeshLoader.h"
#include "M2SkeletonLoader.h"
#include "M2Model.h"

namespace PQGameCore
{
	M2LoadImpl::M2LoadImpl(PQEngine::SceneObject* sceneObject, std::string path)
		: SceneResourceLoadImpl(sceneObject,path),_ok(false)
	{
		assert(_sceneObject);
	}

	PQEngine::GameObject* M2LoadImpl::load()
	{
		std::string skeletonName=M2Model::get(_path)->getSkeletonName();
		std::string meshName=M2Model::get(_path)->getMeshName();

		M2SkeletonLoader* skeletonLoader=new M2SkeletonLoader(_path);
		_sceneObject->createSkeleton(skeletonName,skeletonLoader);

		M2MeshLoader *meshLoader = new M2MeshLoader(_path);

		//first create mesh then load mesh from m2 file
		_sceneObject->createMesh(meshName,meshLoader);
		PQEngine::GameObject* obj= (PQEngine::GameObject*)_sceneObject->createEntityObject(meshName);

		if(meshLoader){
			delete meshLoader;
		}
		if(skeletonLoader){
			delete skeletonLoader;
		}

		return obj;
	}
}
